import axios from 'axios'
// import { Loading, Message } from 'element-ui'
// import router from '../router'
// import { RETURN_CODE, BUSINESS_CODE } from './code'

// axios.defaults.withCredentials = true // 需要凭证
axios.defaults.timeout = 600000

// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
    config => {
        if (localStorage.getItem('Authorization')) {
            config.headers.token = localStorage.getItem('Authorization');
        }
        return config;
    },

)

// 响应拦截器
// axios.interceptors.response.use(null, err => {
//     if (err.response.status === 401) {
//         // 没有登录
//         // 清空缓存的token
//         localStorage.removeItem('Authorization')
//         // 跳转login
//         vm.$router.push("/");
//     }
//     return Promise.reject(err);
// });

// get
export async function fetchGet(url, params = {}) {
    // console.log(url);
    return await axios.get(url, {
        params: params
    })
}
// post
export async function fetchPost(url, data = {}) {
    return await axios.post(url, data)
}
// put
export async function fetchPut(url, data = {}) {
    return await axios.put(url, data)
}

// delete
export async function fetchDel(url, data = { emulateJSON: true }) {
    return await axios.delete(url, data)
}

// getDown   【文件下载】
export async function fetchGetDown(url, params = {}) {
    // console.log(url);
    return await axios.get(url, {
        params: params,
        responseType: "blob",
    });
}
//postUp  【文件分页下载】
export async function fetchPostDown(url, params = {}) {
    // console.log(url);
    return await axios.post(url, params, {
        responseType: "blob",
    });
}
// postUp上传封装函数  【文件上传】
export async function fetchPostUp(url, params = {}) {
    // console.log(url);
    return await axios.post(url, params, {
        responseType: "blob",
    });
}

